import '../css/common.scss';
import '../js/jquery.cookie.js';
import '../js/common.js';
var wx = require('weixin-js-sdk');
import {productlist,qrcode} from './urlconfig/config.js';
var date='';
var appid='';
var signature='';
var str='';
var channel='';
$.ajax({
    type: "get",
    url:productlist ,
    async:false,
    dataType: "json",
    success: function(res){
        if(res.code == 200){
            console.log(res.data);
            date=res.data.wechat_jsapi.date;
            appid=res.data.wechat_jsapi.id;
            signature=res.data.wechat_jsapi.signature;
            str=res.data.wechat_jsapi.str;
            channel=res.data.channel_info.channelCode;
        }else{
            console.log(res.code)
        }
        console.log(res.msg)
    },
    error:function (err){
        console.log(err)
    }
});

wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
    appId: appid, // 必填，公众号的唯一标识
    timestamp: date, // 必填，生成签名的时间戳
    nonceStr: str, // 必填，生成签名的随机串
    signature: signature,// 必填，签名
    jsApiList: [
        "onMenuShareAppMessage",
        "onMenuShareTimeline"
    ] // 必填，需要使用的JS接口列表
});

var url = "http://"+window.location.host+window.location.pathname+"?c_code="+channel;

wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
    wx.onMenuShareAppMessage({ //分享给朋友
        title: '每人每天低至0.23元的Office重磅升级', // 分享标题
        desc: '8款熟悉的应用，为忙到飞起的你简化操作，办公更高效！Skr！Skr！', // 分享描述
        link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: 'http://cdn.office.store.zhuazi.com/pic/wechat/share/share_pic.png', // 分享图标
        type: '', // 分享类型,music、video或link，不填默认为link
        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
        success: function () {
            // 用户点击了分享后执行的回调函数
        }
    });
    wx.onMenuShareTimeline({
        title: '每人每天低至0.23元的Office重磅升级', // 分享标题
        link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: 'http://cdn.office.store.zhuazi.com/pic/wechat/share/share_pic.png', // 分享图标
        success: function () {
            // 用户点击了分享后执行的回调函数
        }
    });
});
/*立即购买*/
$(".subscribe").click(function(){
    var productToken = $(this).attr("data");
    $.ajax({
        type: "post",
        url: qrcode,
        async:false,
        data: {"token":webuser.data.token,"product_token":productToken,"buy_position":"wechat","two_purchase":1,"channel":c_code,"openid":webuser.data.openid},
        dataType: "json",
        success: function(data){
            if(data.code == 200){
                tt = data.data['_time'];
                jsApiParameters = JSON.parse(data.data.jsApiParameters);
                order_token = data.data.order_token;
                callpay();
            }

        }
    });

});

//调用微信JS api 支付
function jsApiCall()
{
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        jsApiParameters,
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ){
                sleep(1500);
                window.location.href="{:U('/Office/pay/wxwebmsg','status=success')}"+"?order_token="+order_token;
            }else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
                sendTagLog("inPaypageCancel",false);
                $(".mask").fadeIn();
                $('.anmo').fadeIn();
                if(hj) {
                    _hmt.push(['_trackEvent','wxpop','view','弹窗吊起数']);
                }
            }else if(res.err_msg == "get_brand_wcpay_request:fail" ){
                sendTagLog("inPaypageCancel",false);
                $(".mask").fadeIn();
                $('.anmo').fadeIn();
                if(hj) {
                    _hmt.push(['_trackEvent','wxpop','view','弹窗吊起数']);
                }
            }
        }
    );
}

function callpay()
{
    if (typeof WeixinJSBridge == "undefined"){

        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
        }
    }else{

        jsApiCall();
    }
}

function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

$(function() {
    var url = window.location.toString();
    var id = url.split('#')[1];
    var locationUrl = "{:U('/weixin/product/splitDetail','','html')}"+"?c_code="+c_code;
    if(id){
        $("#"+id+"_tab").siblings().removeClass("weui-bar__item_on");
        $("#"+id+"_tab").addClass("weui-bar__item_on");
        $("#" + id).siblings().css('display', 'none');
        $("#" + id).css('display', 'block');
        $('html, body').animate({scrollTop: $('#top').offset().top}, 0);
    }

    $('.weui-navbar__item').on('click',function() {
        var id = $(this).attr("data-id");

        $(this).siblings().removeClass("weui-bar__item_on");
        $(this).addClass("weui-bar__item_on");
        $("#" + id).siblings().css('display', 'none');
        $("#" + id).css('display', 'block');

        $('html, body').animate({scrollTop: $('#top').offset().top}, 500);

    });

    $(".anmo-top").click(function (){
        $(".mask").fadeOut();
        $('.anmo').fadeOut();
        if(hj) {
            _hmt.push(['_trackEvent','wxpop','click','其他点击数']);
        }
        window.location.href=locationUrl;
    });
    $(".tip").click(function (){
        $(".mask").fadeOut();
        $('.anmo').fadeOut();
        if(hj) {
            _hmt.push(['_trackEvent','wxpop','click','按钮点击数']);
        }
        window.location.href=locationUrl;
    });
    $(".btn-close").click(function (){
        $(".mask").fadeOut();
        $('.anmo').fadeOut();
        if(hj) {
            _hmt.push(['_trackEvent','wxpop','click','关闭点击数']);
        }
        window.location.href= locationUrl;
    });

});



























// function getQueryStringByName(name)
// {
//     var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
//     if (result == null || result.length < 1)
//     {
//         return "";
//     }
//     return result[1];
// }
// var c_code = getQueryStringByName("c_code");


